/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.test;

import java.util.HashMap;
import java.util.Map;

import org.sss.client.DataGetter;
import org.sss.client.DataPutter;
import org.sss.client.SimpleJob;
import org.sss.client.SssClient;
import org.sss.mapreduce.GroupID;
import org.sss.mapreduce.Mapper;
import org.sss.mapreduce.Reducer;
import org.sss.mapreduce.Tuple;
import org.sss.mapreduce.datatype.PackableBytes;
import org.sss.mapreduce.datatype.PackableInt;
import org.sss.mapreduce.datatype.PackableString;
import org.sss.mapreduce.example.IntSumReducer;
import org.sss.mapreduce.example.TokenizerMapper;

public class WordCountABC {
  static Class<? extends Mapper>   mapperClass   = TokenizerMapper.class;
  static Class<? extends Reducer>  combinerClass = IntSumReducer.class;
  static Class<? extends Reducer>  reducerClass  = IntSumReducer.class;

  public static GroupID run(SssClient client, GroupID input) throws Exception {
    SimpleJob job = new SimpleJob(client);

    job.setMapperClass(mapperClass);
    job.setCombinerClass(combinerClass);
    job.setReducerClass(reducerClass);
    job.setInputGID(input);

    return job.execute();
  }

  /**
   * Usage:
   */
  public static void main(String[] args) throws Exception {
    SssClient client = new SssClient(args);
    long start;
    long end;
    long elapsed;

    String[] others = client.getArgs();
    if (others.length < 1) {
      System.err.println("no recod size");
      System.exit(1);
    }
    int n = Integer.parseInt(others[0]);


    PackableBytes val;
    {
      StringBuilder sb = new StringBuilder();
      for (char c = 'a';c <= 'z';++c) {
        for (int i = 0;i < 5;++i) {
          for (int j = 0;j < 5;++j) {
            sb.append(c);
          }
          sb.append(' ');
        }
        sb.append('\n');
      }
      val = new PackableBytes(sb.toString().getBytes("UTF-8"));
    }

    // Upload
    //
    start = System.currentTimeMillis();
    DataPutter<PackableBytes, PackableBytes>  putter = 
      DataPutter.create(client, PackableBytes.class, PackableBytes.class);
    GroupID table = putter.getOutputGroupID();
    try {
      for (int i = 0;i < n;) {
        int stop  = Math.min(i + 4096, n);
        for (;i < stop;++i) {
          putter.append(new PackableBytes(Integer.toString(i).getBytes("UTF-8")), val);
        }
      }
    } finally {
      putter.close();
    }

    end = System.currentTimeMillis();
    elapsed = end - start;

    System.out.println("Data upload(" + table + ") elapsed " + elapsed + " ms.");

    // Word Count
    start = System.currentTimeMillis();
    GroupID out = run(client, table);
    end = System.currentTimeMillis();
    elapsed = end - start;

    System.out.println("Word count elapsed " + elapsed + " ms." + out.toString());

    // Get Result
    start = System.currentTimeMillis();

    Map<String, Integer> result = new HashMap<String, Integer>();
    DataGetter<PackableString, PackableInt> getter = 
      DataGetter.create(client, PackableString.class, PackableInt.class, out);
    try {
      for (Tuple<PackableString, PackableInt> kv: getter) {
        String word = kv.getKey().get();
        int cnt = kv.getValue().get();

        if (result.containsKey(word)) {
          System.err.println(word + " duplicate.");
        } else {
          result.put(word, cnt);
        }
      }
    } finally {
      getter.close();
    }

    end = System.currentTimeMillis();
    elapsed = end - start;

    System.out.println("\"Gets result\" elapsed " + elapsed + " ms.");
    
    Map<String, Integer> expect = new HashMap<String, Integer>();
    for (char c = 'a';c <= 'z';++c) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0;i < 5;++i) {
        sb.append(c);
      }
      expect.put(sb.toString(), n * 5);
    }

    if (result.equals(expect)) {
      System.out.println("Expect result.");
    } else {
      System.err.println("Unexpect result.");
    }
  }
}
